package icraf.carbon.gui;

import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.common.gui.MenuGenerator.IconModel;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class AboutAbacus extends JPanel {
	private static final long serialVersionUID = -5209256927070082594L;
	private Image icrafLogo;
	private Image abacusBanner;
	private Image euLogo;
	private Image allreddiLogo;
	Font fontTitle = new Font("Helvetica", 1, 50);
	Font fontVersion = new Font("Arial", Font.PLAIN, 12);
	Font fontDesc = new Font("Arial", Font.PLAIN, 8);
	Font fontLogo = new Font("Arial", Font.PLAIN, 10);
	private boolean isFullType = false;

	public AboutAbacus(boolean isFullType) {
		super();
		this.isFullType = isFullType;
		ImageIcon icon = MenuFactory.getAbacusBanner();
		abacusBanner = icon.getImage();
		if (isFullType) {
			initFullType();
		}

	}

	public static void main(String[] args) {
		AboutAbacus p = new AboutAbacus(true);
		final JDialog d = p.createDialog(null, ModalityType.MODELESS);
		d.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}

		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				d.setVisible(true);
			}
		});
		
	}

	public static String appTitle = "REDD Abacus SP";
	public static String appSubTitle1 = "ABAtement Cost cUrveS";
	public static String appSubTitle2 = "for Reducing Emission from Deforestation and Degradation";
	public static String appVersion = "Version 1.1.5 beta3";
	public static String appCopyright = "\u00a9 World Agroforestry Center 2014";

	public static String aboutText = "<html><font size='2'>an <font size='4'><b>ABA</b>tement <b>C</b>ost c<b>U</b>rve<b>S</b></font> generator<br>"
			+ "for <font size='4'><b>REDD</b></font> opportunity cost analysis<br>"
			+ "includes <font size='3'><b>S</b>imulator for <b>P</b>olicy Scenarios</font><br><br>"
			+ "for more information about the software, please refer to:<br><font size='2'><b>REDD Abacus SP - User Manual</b></font><br><br>"
			+ "website:<br><font size='3'>http://code.google.com/p/redd-abacus/</font></font></html>";


	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		Dimension d = getSize();
		int offsetX = d.width / 2 - 200;
		int offsetY = isFullType ? -20 : d.height / 2 - 150;

		GradientPaint painter = new GradientPaint(0.0f, 0, Color.black, 0.0f,
				d.height, new Color(240, 240, 240));
		g2.setPaint(painter);
		g2.fillRect(0, 0, (int) d.width, (int) d.getHeight());

		drawReflection(g2, abacusBanner, offsetX, offsetY + 40, 1 / 3f, 0, this);
		g2.setFont(fontVersion);
		g2.setColor(new Color(240, 240, 240));
		g2.drawString(appVersion, offsetX + 310, offsetY + 70);
		g2.drawString(appCopyright, offsetX + 180, offsetY + 150);

		if (isFullType) {

			int yImage = this.getHeight() - 110;
			int xImage = offsetX - 10;
			g2.drawImage(icrafLogo, xImage, yImage, this);
			g2.drawImage(allreddiLogo, xImage + 170, yImage, this);
			g2.drawImage(euLogo, xImage + 300, yImage + 30, this);
			g2.setColor(Color.darkGray);
			g2.setFont(fontLogo);
			g2.drawString("Supported by:", xImage + 298, yImage + 25);
			g2.drawString("European Union", xImage + 295, yImage + 90);
		}
	}

	private static void drawReflection(Graphics2D g2d, Image avatar, int x,
			int y, float ratio, int distance, ImageObserver observer) {

		int avatarWidth = avatar.getWidth(observer);
		int avatarHeight = avatar.getHeight(observer);

		// do that in the constructor ...
		BufferedImage reflection = new BufferedImage(avatarWidth, avatarHeight,
				BufferedImage.TYPE_INT_ARGB);
		Graphics2D reflectionGraphics = reflection.createGraphics();

		AffineTransform tranform = AffineTransform.getScaleInstance(1.0, -1.0);
		tranform.translate(0, -avatarHeight);
		// draw the flipped image
		reflectionGraphics.drawImage(avatar, tranform, observer);

		GradientPaint painter = new GradientPaint(0.0f, 0.0f, new Color(0.0f,
				0.0f, 0.0f, 0.5f), 0.0f, avatarHeight * ratio, new Color(0.0f,
				0.0f, 0.0f, 1.0f));

		// this use : Ar = Ad*(1-As) and Cr = Cd*(1-As)
		reflectionGraphics.setComposite(AlphaComposite.DstOut);
		reflectionGraphics.setPaint(painter);
		// this will make our image transluent ...
		reflectionGraphics.fill(new Rectangle2D.Double(0, 0, avatarWidth,
				avatarHeight));

		reflectionGraphics.dispose();

		g2d.drawImage(avatar, x, y, observer);
		// using the default composite method, this will merge the reflection
		// with the background
		g2d.drawImage(reflection, x, y + avatarHeight + distance, observer);
	}

	public void initFullType() {
		ImageIcon icon = MenuFactory.getIcrafImage();
		icrafLogo = icon.getImage();
		icon = MenuFactory.getEUImage();
		euLogo = icon.getImage();
		icon = MenuFactory.getAllreddiImage();
		allreddiLogo = icon.getImage();
	}

	public JDialog createDialog(Window owner, ModalityType modalityType) {
		JLabel a = new JLabel();
		a.setFont(fontDesc);
		a.setText(aboutText);
		a.setPreferredSize(new Dimension(300, 330));
		a.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		a.setBounds(60, 130, 400, 160);
		a.setForeground(Color.white);

		this.setBounds(0, 0, 500, 400);
		JLayeredPane chartPane = new JLayeredPane();
		chartPane.add(a);
		chartPane.add(this);

		chartPane.setPreferredSize(new Dimension(500, 400));

		JDialog dialog = new JDialog(owner, modalityType);

		dialog.setResizable(false);
		dialog.getContentPane().add(chartPane);
		dialog.setTitle("About " + appTitle);
		dialog.setIconImage(MenuProperty.GENERAL_ABOUT
				.getIcon(IconModel.ICON16).getImage());
		dialog.pack();
		return dialog;
	}



}
